package com.ruoyi.patrol.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.entity.PtDMLParam;
import com.ruoyi.common.enums.UnitType;
import com.ruoyi.common.service.DDLService;
import com.ruoyi.patrol.constants.PatrolConstant;
import com.ruoyi.patrol.domain.dto.PtLocusDTO;
import com.ruoyi.patrol.service.IPtWorkLocusService;
import com.ruoyi.patrol.util.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class IPtWorkLocusServiceImpl implements IPtWorkLocusService {

    @Autowired
    private DDLService ddlService;

    @Override
    public List<Map<String, Object>> selectPtWorkLocusList(PtLocusDTO locusDTO) {
        Long deptId = locusDTO.getDeptId();
        List<Map<String, Object>> list = ddlService.list("pt_locus_" + deptId, getLocusParams(locusDTO), null);
        return DataUtil.locusDataUnCover(list);
    }

    @Override
    public int insertPtWorkLocus(Map<String, Object> param) {
        String table = "pt_locus_" + MapUtil.getStr(param, "deptId");
        List<Map<String, Object>> data = (List<Map<String, Object>>) param.get("data");
        Map<String, Object> map = new HashMap<>();
        map.put("user", param.get("user"));
        map.put("work_id", param.get("workId") == null ? IdUtil.getSnowflakeNextId() : param.get("workId"));
        map.put("work_type", param.get("workType"));
        List<Map<String, Object>> out = DataUtil.locusDataCover(data, map);
        return ddlService.insert(table, out);
    }

    @Override
    public int deletePtWorkLocus(PtLocusDTO locusDTO) {
        Long deptId = locusDTO.getDeptId();
        return ddlService.delete("pt_locus_" + deptId, getLocusParams(locusDTO));
    }


    private List<PtDMLParam> getLocusParams(PtLocusDTO locusDTO) {
        List<PtDMLParam> ptDMLParams = new ArrayList<>();
        ptDMLParams.add(PtDMLParam.getPtDMLParam("user", UnitType.EQ, locusDTO.getUser()));
        ptDMLParams.add(PtDMLParam.getPtDMLParam("work_id", UnitType.EQ, locusDTO.getWorkId()));
        ptDMLParams.add(PtDMLParam.getPtDMLParam("work_type", UnitType.EQ, locusDTO.getType()));
        return ptDMLParams;
    }
}
